package com.buobao.bmall.common;

import com.buobao.bmall.exception.ExceptionEnum;

public class ApiRestResponse<T> {
    private static final Integer OK = 200;
    private static final String MSG_OK = "SUCCESS";

    private Integer status;
    private String msg;
    private T data;

    public ApiRestResponse(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ApiRestResponse(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ApiRestResponse() {
        this(OK,MSG_OK);
    }

    public static <T> ApiRestResponse<T> success(T data) {
        return new ApiRestResponse<>(OK,MSG_OK,data);
    }

    public static <T> ApiRestResponse<T> success() {
        return new ApiRestResponse<>(OK,MSG_OK);
    }

    public static <T> ApiRestResponse<T> error(Integer code, String msg) {
        return new ApiRestResponse<>(code,msg);
    }

    public static <T> ApiRestResponse<T> error(ExceptionEnum exceptionEnum) {
        return new ApiRestResponse<>(exceptionEnum.getCode(),exceptionEnum.getMsg());
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ApiRestResponse{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data.toString() +
                '}';
    }
}
